<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/17
 * Time: 10:26
 */
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;
use think\db\Expression;

class PropsController extends BaseController{
    const AWTYPES = [1 => '普通', 2 => '特殊'];  // 道具类型
    public function indexAction (){
        $order = [];
        $CHANNELS   = $this->GetAdminChannel();
        $a=array_values($CHANNELS);
        $b= $a[0]['ID'];
        $channel = $this->request->param('channel/d',$b);
        if ($channel > 0 && array_key_exists($channel, $CHANNELS)) {
            $conds['Channel'][] = ['IN', array_keys($CHANNELS)];
            $conds['Channel'][] = ['EQ', $channel];
        } else {
            $conds['Channel'] = ['IN', array_keys($CHANNELS)];
        }
        $orderMap = [
            'A' => 'ID',
            'B' => 'Channel',
            'C' => 'PropsIndex',
            'E' => 'PropsType',
            'F' => 'Cash',
            'G' => 'Gold',
            'H' => 'RoomCard',
        ];
        $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'A';
        $order[$orderMap[$this->orderField]] = $this->orderDirection;
        $list = table('GameProps', 'rydb.basic')->where($conds)->order($order)->paginate();
        return $this->fetch('', [
            'list'    => $list,
            'channel' => $channel,
            'AWTYPES' => self::AWTYPES,
            'CHANNELS'  => $CHANNELS,
        ]);
    }
    /**
     *  @brief  : 修改道具
     *  @return : page|JSON
     *  @details:
     */
    public function updatepropsAction(){
        $propsID = $this->request->param('ID');
        $status = $this->request->param('Status');
        if (!empty($status)){
            if ($status == 1) {
                table('GameProps', 'rydb.basic')->where(['ID' => $propsID])->update(['Status' => 2]);
                return result([], 0);
            }else{
                table('GameProps', 'rydb.basic')->where(['ID' => $propsID])->update(['Status' => 1]);
                return result([], 0);
            }
        }
    }

    /**
     *  @brief  : 道具详情
     *  @return : page|JSON
     *  @details:
     */
    public function detailAction()
    {
        $CHANNELS = $this->GetAdminChannel();
        $GAME_KIND = GetGameKind();
        $item = [];
        $id = $this->request->param('id/d', 0);
        if ($id > 0) {
            $item = table('GameProps', 'rydb.basic')->where([
                'Channel' => ['IN', array_keys($CHANNELS)],
                'ID' => ['EQ', $id],
            ])->find();
        }
        if ($this->request->isGet()) {
            if (empty($item)) {
                return abort(100, "不存在该道具ID={$id}");
            }
            return $this->fetch('', [
                'item' => $item,
                'CHANNELS' => $CHANNELS,
                'GAME_KIND' => $GAME_KIND,
                'AWTYPES' => self::AWTYPES,
            ]);
        }
        if ($this->request->isPost()) {
            if (empty($item)) {
                return result([], 100, "不存在该道具ID={$id}");
            }
            $data = $this->request->only('PropsName,PropsIndex,PropsType,Gold,Card,GameProsName');
            //--- 验证器验证 ---
            $retn = $this->validate($data, [ // 按照顺序逐一判断
                'PropsName|道具名称'    => 'require|max:31',
                'PropsIndex|道具索引'   => 'require|integer|egt:20',
                'PropsType|道具类型'    => 'require|in:1,2',
                'Gold|金币价值'         => 'require|integer|egt:0',
                'Card|房卡游戏'         => 'require|integer|egt:0',
                'GameProsName|备注描述' => 'max:100',
            ]);
            if (true !== $retn) {
                return result([], 101, $retn);
            }

            $eax = table('GameProps', 'rydb.basic')->where('ID', $id)->update($data);
            return result([], 0);
        }
    }

}